$(document).ready(function () {
    var path = getRootPath();
    initPost();

    $("#btn_search").click(function () {
        $("#btn_search").linkbutton('disable');
        var queryParams = $('#queryForm').serializeJSON();
        queryParams.onLoadSuccess = function (data) {
            $("#btn_search").linkbutton('enable');
        };
        $('#tbl_post_detail').datagrid('load', queryParams);
    });


    //打开增加对话框
    $("#btn_addPost").click(function () {
        console.log("打开增加对话框");
        $('#dlg_postSave').dialog({title: '增加'});
        $("#dlg_postSave").dialog("open");
    });
//删除
    $("#btn_delPost").click(function () {
        var row = $('#tbl_post_detail').datagrid("getSelected");
        if (null == row) {
            $.messager.alert('警告', '请选中要删除的行', 'warning', function () {
                return false;
            });
        } else {
            $.messager.confirm('确认', '你确定要删除‘' + row.postName + '’么？', function (r) {
                if (r) {
                    $("#btn_delDepart").linkbutton('disable');
                    var url = path + "/sysset/delPost";
                    var postData = {"postId": row.postId};
                    $.post(url, postData, function (data) {
                        var mess = data.mess;
                        if (mess == "succ") {
                            $.messager.alert('消息', '删除成功');
                            $('#dlg_postSave').dialog('close');
                            $('#tbl_post_detail').datagrid('load');
                            $("#btn_delPost").linkbutton('enable');
                        } else {
                            $.messager.alert('警告', mess, 'warning', function () {
                                $("#btn_delPost").linkbutton('enable');
                            });
                        }
                    });
                }
            });
        }
    });
//对话框本体
    $("#btn_editPost").click(function () {
        $('#dlg_postSave').dialog({title: '修改'});
        var row = $('#tbl_post_detail').datagrid("getSelected");
        if (null == row) {
            $.messager.alert('警告', '请选中要修改的行', 'warning', function () {
                return false;
            });
        } else {
            $("#add_postName").textbox('setValue', row.postName);
            $("#add_postExplain").textbox('setValue', row.postExplain);
            ////被选择的部门
            $("#add_postId").val(row.postId);
            $("#dlg_postSave").dialog("open");
        }
    });

    /*
     保存
     */
    $("#btn_savepost").click(function () {
        var postName = $("#add_postName").textbox('getValue');
        if (postName == "") {
            $.messager.alert('警告', '职务名称不能为空!', 'warning', function () {
                $("#add_postName").next('span').find('input').focus();
                return false;
            });
        } else {
            $("#btn_savepost").linkbutton('disable');
            var postId = $("#add_postId").val()
            var url = "";
            if (postId == "")
                url = path + "/sysset/addPost";
            else
                url = path + "/sysset/updPost";
            var postData = $("#saveFormPost").serializeJSON();
            $.post(url, postData, function (data) {
                mess = eval(data).mess;
                if (mess == "succ") {
                    if (postId == "")
                        $.messager.alert('消息', '保存成功');
                    else
                        $.messager.alert('消息', '修改成功');
                    $('#dlg_postSave').dialog('close');
                    $("#btn_savePost").linkbutton('enable');
                    $('#tbl_post_detail').datagrid('load');
                    clearForm();
                } else {
                    $.messager.alert('警告', mess, 'warning', function () {
                        $("#btn_savePost").linkbutton('enable');
                    });
                }
            });
        }
    });

    /*
    * 清除对话框内容
    *
    * */
    $("#btn_cancelpost").click(function () {
        clearForm();
        $("#dlg_postSave").dialog("close");
    });


    function clearForm() {
        $("#add_postName").textbox('setValue', "");
        $("#add_postExplain").textbox('setValue', "");
        $("#add_postId").val("");
    }

    function initPost() {
        var url = path + "/sysset/queryPost";
        //data-options="border:false,singleSelect:true,fit:true,fitColumns:true,rownumbers:true"
        $('#tbl_post_detail').datagrid({
            border: false,
            singleSelect: true,
            fit: true,
            fitColumns: true,
            rownumbers: true,
            autoRowHeight: false,
            nowrap: true,
            loadMsg: "正在加载，请稍后...",
            striped: true,
            url: url,
            pagination: true,
            pageSize: 10,
            pageNumber: 1,
            columns: [[
                {field: 'postName', title: '岗位名称', width: 100},
                {field: 'postExplain', title: '岗位简介', width: 100},
            ]],
            onBeforeLoad: function (param) {
                var pageNo = param.page;
                delete param.page;
                param.pageNo = pageNo;

                var maxResults = param.rows;
                delete param.rows;
                param.maxResults = maxResults;
            },
            onLoadSuccess: function (data) {
                //alert(data);
            }
        });
    }
})